home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / ecstr2.arc / STRFIELD.C < prev    next >
C/C++ Source or Header  |  1987-03-04  |  3KB  |  70 lines

  1. /*  File   : strfield.c
  2.     Author : Richard A. O'Keefe.
  3.     Updated: 21 April 1984
  4.     Defines: strfield()
  5.  
  6.     strfield(src, fields, chars, blanks, tabch)
  7.        is based on the key specifications of the sort(1) command.
  8.  
  9.        tabch corresponds to 'x' in -t'x'.  If it is NUL, a field
  10.        is leading layout (spaces, tabs &c) followed by at least
  11.        one non-layout character, and is terminated by the next
  12.        layout character or NUL.  If it is not NUL, a field is
  13.        terminated by tabch or NUL.
  14.  
  15.      fields is the number of fields to skip over.  It corresponds
  16.        to m in -m.n or +m.n .  There must be at least this many
  17.        fields, and only the last may be terminated by NUL.
  18.  
  19.        chars is the number of characters to skip after the fields
  20.        have been skipped.  At least this many non-NUL characters
  21.        must remain after the fields have been skipped.  Note that
  22.        it is entirely possible for this skip to cross one or more
  23.        field boundaries.  This corresponds to n in +m.n or -m.n .
  24.  
  25.        Finally, if blanks is not 0, any layout characters will be
  26.        skipped.  There need not be any.  This corresponds to the
  27.        letter b in +2.0b or -0.4b .
  28.  
  29.        The result is NullS if the source ran out of fields or ran
  30.        out of chars.  Otherwise it is a pointer to the first
  31.        character of src which was not skipped.  It is quite possible
  32.        for this character to be the terminating NUL.
  33.  
  34.     Example:
  35.        to skip to the user-id field of /etc/passwd:
  36.            user_id = strfield(line, 2, 0, 0, ':');
  37.  
  38.      to check whether "line" is at least 27 characters long:
  39.            if (strfield(line, 0, 27, 0, 0)) then-it-is;
  40.  
  41.        to select the third blank-delimited field in a line:
  42.            head = strfield(line, 2, 0, 1, 0);
  43.            tail = strfield(head, 1, 0, 0, 0);
  44.            (* the field is the tail-head characters starting at head *)
  45.  
  46.     It's not a bug, it's a feature: "layout" means any ASCII character
  47.        in the range '\1' .. ' ', including '\n', '\f' and so on.
  48. */
  49.  
  50. #include "strings.h"
  51.  
  52. char *strfield(src, fields, chars, blanks, tabch)
  53.     register char *src;
  54.     int fields, chars, blanks, tabch;
  55.     {
  56.        if (tabch <= 0) {
  57.            while (--fields >= 0) {
  58.                while (*src <= ' ') if (!*src++) return NullS;
  59.                while (*++src > ' ') ;
  60.            }
  61.      } else
  62.        if (fields > 0) {
  63.            do if (!*src) return NullS;
  64.            while (*src++ != tabch || --fields > 0);
  65.        }
  66.        while (--chars >= 0) if (!*src++) return NullS;
  67.        if (blanks) while (*src && *src++ <= ' ') ;
  68.        return src;
  69.     }
  70.